import redis_uitl
import time
from datetime import datetime

# 配置 Redis 哨兵连接信息和主节点名称
REDIS_OTHER_HOST = "192.168.144.131"
REDIS_OTHER_PORT = 6379
REDIS_OTHER_PASSWORD = "123456"

REDIS_CURRENT_HOST = "192.168.144.130"
REDIS_CURRENT_PORT = 6379
REDIS_CURRENT_PASSWORD = "123456"

log_info_file = open('/proc/1/fd/1', 'w')
log_error_file = open('/proc/1/fd/2', 'w')

      
if __name__ == "__main__":
    print(f"{datetime.now()}== 触发本机更改为VIP ==start", file=log_info_file)
    if(redis_uitl.check_ip_port(REDIS_OTHER_HOST,REDIS_OTHER_PORT)):
        print(f"{datetime.now()}== 先将{REDIS_CURRENT_HOST}变为从，用来拷贝数据", file=log_info_file)
        redis_uitl.to_slave(
            REDIS_OTHER_HOST,
            REDIS_OTHER_PORT,
            REDIS_OTHER_PASSWORD,
            REDIS_CURRENT_HOST,
            REDIS_CURRENT_PORT,
            REDIS_CURRENT_PASSWORD,
        );
    time.sleep(3) # 等待3秒
    if(redis_uitl.check_ip_port(REDIS_CURRENT_HOST,REDIS_CURRENT_PORT)):
        print(f"{datetime.now()}== 再将{REDIS_CURRENT_HOST}变为主", file=log_info_file)
        redis_uitl.to_master(
            REDIS_CURRENT_HOST,
            REDIS_CURRENT_PORT,
            REDIS_CURRENT_PASSWORD
        );
    print(f"{datetime.now()}== 触发本机更改为VIP ==end", file=log_info_file)